//
//  MyPrivateMessageChatRoomVC.m
//  OnionHome
//
//  Created by fengjie on 2019/10/14.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "MyPrivateMessageChatRoomVC.h"

@interface MyPrivateMessageChatRoomVC ()<UITableViewDelegate,UITableViewDataSource>
/**<#属性#>*/
@property (nonatomic,strong)UITableView*listView;
/**<#属性#>*/
@property (nonatomic,strong)UITextField*textField;
@end

@implementation MyPrivateMessageChatRoomVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    
}
-(void)createUI{
    UIView * bomView = [self createTexFileView];
    [self.view addSubview:self.listView];
    [self.view addSubview:bomView];
    
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-50);
    }];
    [bomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.bottom.mas_equalTo(-8);
        make.top.mas_equalTo(self.listView.mas_bottom).offset(8);
    }];
}
-(void)setModel:(MyPrivateMessageModel *)model{
    _model = model;
    self.title = model.userName;
}
#pragma mark UITableViewDelegate,UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.model.modelArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChatRoomLeftCell * leftCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ChatRoomLeftCell class])];
    ChatRoomRightCell * rightCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ChatRoomRightCell class])];
    MyPrivateMessageModel * model = self.model.modelArray[indexPath.row];
    
    if (model.isMe) {
        rightCell.model = model;
        return rightCell;
    }
    leftCell.model = model;
    return leftCell;
}
-(UITableView*)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 44.0f;
        [_listView registerClass:[ChatRoomLeftCell class] forCellReuseIdentifier:NSStringFromClass([ChatRoomLeftCell class])];
        [_listView registerClass:[ChatRoomRightCell class] forCellReuseIdentifier:NSStringFromClass([ChatRoomRightCell class])];

        _listView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _listView;
}

-(UITextField*)textField{
    if(!_textField){
        _textField=[UITextField new];
        _textField.placeholder = @"输入消息…";
    }
    return _textField;
}
-(UIView * )createTexFileView{
    UIView * view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    
    UIView * view1 = UIView.new;
    view1.backgroundColor = UIColorFromRGB(0xF5F7F5);
    ViewRadius(view1, 4);
    
    [view addSubview:view1];
    
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.centerY.mas_equalTo(0);
        make.height.mas_equalTo(30);
    }];
    
    UIImageView * leftImage = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"xiepinglun")];
    [view1 addSubview:leftImage];
    [view1 addSubview:self.textField];
    
    [leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(8);
        make.width.height.mas_equalTo(20);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(leftImage.mas_right).offset(5);
        make.top.right.bottom.mas_equalTo(0);
    }];
    
    return view;
}
@end


@interface ChatRoomLeftCell ()
/**用户头像*/
@property (nonatomic,strong)UIImageView*iconImage;
/**消息内容*/
@property (nonatomic,strong)UILabel*messageLabel;
@end

@implementation ChatRoomLeftCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}
-(void)setModel:(MyPrivateMessageModel *)model{
    _model = model;
    
    self.iconImage.image = IMAGE_NAMED(model.userIcon);
//    self.messageLabel.text = model.message;
    self.messageLabel.attributedText = [TitleTool getAttributeStringLineSpacing:4 title:model.message titleColor:UIColorFromRGB(0x273323) titleFont:SYSTEMFONT(14)];
}
-(void)createUI{
    UIView * bgView = UIView.new;
    ViewRadius(bgView, 5);
    bgView.backgroundColor = UIColorFromRGB(0xF5F7F5);
    
    [self addSubview:self.iconImage];
    [self addSubview:bgView];
    [bgView addSubview:self.messageLabel];
    
    [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(16);
        make.width.height.mas_equalTo(46);
    }];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImage.mas_right).offset(10);
        make.top.mas_equalTo(self.iconImage);
        make.bottom.mas_equalTo(-16);
    }];
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(8);
        make.top.mas_equalTo(12);
        make.right.mas_equalTo(-8);
        make.bottom.mas_equalTo(-12);
        make.width.lessThanOrEqualTo(@(KScreenWidth-92-113));//最大宽度
    }];
}
#pragma mark 懒加载

-(UIImageView*)iconImage{
    if(!_iconImage){
        _iconImage=[UIImageView new];
        _iconImage.image = IMAGE_NAMED(@"userIconDefut");
        ViewRadius(_iconImage, 23);
    }
    return _iconImage;
}
-(UILabel*)messageLabel{
    if(!_messageLabel){
        _messageLabel=[UILabel new];
        _messageLabel.textColor = UIColorFromRGB(0x273323);
        _messageLabel.font = SYSTEMFONT(14);
        _messageLabel.numberOfLines = 0;
    }
    return _messageLabel;
}
@end


@interface ChatRoomRightCell ()

/**用户头像*/
@property (nonatomic,strong)UIImageView*iconImage;
/**消息内容*/
@property (nonatomic,strong)UILabel*messageLabel;

@end

@implementation ChatRoomRightCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}
-(void)setModel:(MyPrivateMessageModel *)model{
    _model = model;
    self.iconImage.image = IMAGE_NAMED(model.userIcon);
//    self.messageLabel.text = model.message;
    self.messageLabel.attributedText = [TitleTool getAttributeStringLineSpacing:4 title:model.message titleColor:UIColorFromRGB(0x273323) titleFont:SYSTEMFONT(14)];

}
-(void)createUI{
    UIView * bgView = UIView.new;
    ViewRadius(bgView, 5);
    bgView.backgroundColor = UIColorFromRGB(0xF5F7F5);
    
    [self addSubview:self.iconImage];
    [self addSubview:bgView];
    [bgView addSubview:self.messageLabel];
    
    [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.width.height.mas_equalTo(46);
    }];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.iconImage.mas_left).offset(-10);
        make.top.mas_equalTo(self.iconImage);
        make.bottom.mas_equalTo(-16);
    }];
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(8);
        make.top.mas_equalTo(12);
        make.right.mas_equalTo(-8);
        make.bottom.mas_equalTo(-12);
        make.width.lessThanOrEqualTo(@(KScreenWidth-92-113));//最大宽度
    }];
}
#pragma mark 懒加载

-(UIImageView*)iconImage{
    if(!_iconImage){
        _iconImage=[UIImageView new];
        _iconImage.image = IMAGE_NAMED(@"userIconDefut");
        ViewRadius(_iconImage, 23);
    }
    return _iconImage;
}
-(UILabel*)messageLabel{
    if(!_messageLabel){
        _messageLabel=[UILabel new];
        _messageLabel.textColor = UIColorFromRGB(0x273323);
        _messageLabel.font = SYSTEMFONT(14);
        _messageLabel.numberOfLines = 0;
    }
    return _messageLabel;
}

@end
